home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 1.iso / toolbox / src / tutorials / custEducation / opengl1 / answers / solar8.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-11-11  |  8.1 KB  |  362 lines

  1. /*
  2.  * Copyright 1993, 1996, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17.  
  18. /* solar8.c - a simple solar system.  
  19.  *    Added a reshape handler to reset the viewport and viewing volume.
  20.  *    Add depth buffering and back face culling.
  21.  *    Add a viewing transformation and control it using mouse input.
  22.  *
  23.  *    F1 key            - print help information
  24.  *    Left Mouse Button    - change incidence and azimuth angles
  25.  *    Middle Mouse Button    - change the twist angle based on
  26.  *                  horizontal mouse movement
  27.  *    Right Mouse Button    - zoom in and out based on vertical
  28.  *                  mouse movement
  29.  *    <R> Key            - reset viewpoint
  30.  *    SPACE Key        - toggle between solid/wireframe models
  31.  *    Escape Key        - exit program
  32.  */
  33. #include <GL/gl.h>
  34. #include <GL/glu.h>
  35. #include <GL/glut.h>
  36.  
  37. #include <stdio.h>
  38. #include <math.h>
  39.  
  40. /* Function Prototypes */
  41.  
  42. GLvoid initgfx( GLvoid );
  43. GLvoid keyboard( GLubyte, GLint, GLint );
  44. GLvoid specialkeys( GLint, GLint, GLint );
  45. GLvoid mouse( GLint, GLint, GLint, GLint );
  46. GLvoid motion( GLint, GLint );
  47. GLvoid reshape( GLsizei, GLsizei );
  48. GLvoid drawScene( GLvoid );
  49.  
  50. void checkError( char * );
  51. void printHelp( char * );
  52.  
  53. void resetView( GLvoid );
  54. void polarView( GLfloat, GLfloat, GLfloat, GLfloat);
  55.  
  56. /* Global Variables */
  57.  
  58. static char *progname; 
  59.  
  60. static GLboolean filledFlag = GL_TRUE;
  61.  
  62. static enum        actions { MOVE_EYE, TWIST_EYE, ZOOM, MOVE_NONE };
  63. static GLint        action;
  64.  
  65. static GLdouble        xStart = 0.0, yStart = 0.0;
  66.  
  67. static GLfloat        sunRadius = 0.7;
  68. static GLfloat        earthRadius = 0.4, earthOrbit = 3.5; 
  69. static GLfloat        moonRadius = 0.2, moonOrbit = 1.0;
  70. static GLfloat         near, far, distance, twistAngle, incAngle, azimAngle;
  71.  
  72. /* Global Definitions */
  73.  
  74. #define KEY_ESC    27    /* ascii value for the escape key */
  75.  
  76. void
  77. main( int argc, char *argv[] )
  78. {
  79.     GLsizei width, height;
  80.  
  81.     glutInit( &argc, argv );
  82.  
  83.     /* create a window that is 1/4 the size of the screen,
  84.      * and position it in the middle of the screen.
  85.      */
  86.     width = glutGet( GLUT_SCREEN_WIDTH ); 
  87.     height = glutGet( GLUT_SCREEN_HEIGHT );
  88.     glutInitWindowPosition( width / 4, height / 4 );
  89.     glutInitWindowSize( width / 2, height / 2 );
  90.     glutInitDisplayMode( GLUT_RGBA | GLUT_DEPTH );
  91.     glutCreateWindow( argv[0] );
  92.  
  93.     initgfx();
  94.  
  95.     glutReshapeFunc( reshape );
  96.     glutKeyboardFunc( keyboard );
  97.     glutSpecialFunc( specialkeys );
  98.     glutMouseFunc( mouse );
  99.     glutMotionFunc( motion );
  100.     glutDisplayFunc( drawScene ); 
  101.  
  102.     progname = argv[0];
  103.  
  104.     printHelp( progname );
  105.  
  106.     glutMainLoop();
  107. }
  108.  
  109. void
  110. printHelp( char *progname )
  111. {
  112.     fprintf(stdout, 
  113.         "\n%s - model some objects\n\n"
  114.         "F1 key        - print help information\n"
  115.         "Left Mousebutton    - move eye position\n"
  116.         "Middle Mousebutton    - change twist angle\n"
  117.         "Right Mousebutton    - move up / down to zoom in / out\n"
  118.         "<R> Key        - reset viewpoint\n"
  119.         "SPACE key    - toggle between solid/wireframe mode\n"
  120.         "Escape Key    - exit the program\n\n",
  121.         progname);
  122. }
  123.  
  124. GLvoid
  125. initgfx( GLvoid )
  126. {
  127.     GLfloat maxOrbit;
  128.  
  129.     /* set clear color to black */
  130.     glClearColor( 0.0, 0.0, 0.0, 1.0 );
  131.  
  132.     /* enable the depth buffer */
  133.     glEnable( GL_DEPTH_TEST );
  134.  
  135.     /* enable the face culling */
  136.     glEnable( GL_CULL_FACE );
  137.  
  138.     /* Maximum size of all the objects in your scene */
  139.     maxOrbit = earthOrbit + moonOrbit + moonRadius;
  140.  
  141.     /* Set up near and far so that ( far - near ) > maxObjectSize, */
  142.     /* and determine the viewing distance (adjust for zooming) */
  143.     near = 1.0;
  144.     far = near + 8*maxOrbit; 
  145.  
  146.     resetView();
  147. }
  148.  
  149. GLvoid 
  150. reshape( GLsizei width, GLsizei height )
  151. {
  152.     GLdouble    aspect;
  153.  
  154.     glViewport( 0, 0, width, height );
  155.  
  156.     /* compute aspect ratio */
  157.     aspect = (GLdouble) width / (GLdouble) height;
  158.  
  159.     glMatrixMode( GL_PROJECTION );
  160.  
  161.     /* Reset world coordinates first ... */
  162.     glLoadIdentity();
  163.  
  164.     /* Reset the viewing volume based on the new aspect ratio */
  165.     gluPerspective( 45.0, aspect, near, far );
  166.  
  167.     glMatrixMode( GL_MODELVIEW );
  168. }
  169.  
  170. void 
  171. checkError( char *label )
  172. {
  173.     GLenum error;
  174.     while ( (error = glGetError()) != GL_NO_ERROR )
  175.         printf( "%s: %s\n", label, gluErrorString(error) );
  176. }
  177.  
  178. GLvoid 
  179. keyboard( GLubyte key, GLint x, GLint y )
  180. {
  181.     switch (key) {
  182.     case ' ':    /* toggle fill mode */
  183.         filledFlag = !filledFlag;
  184.         glutPostRedisplay();
  185.         break;
  186.     case 'R':
  187.         resetView();
  188.         glutPostRedisplay();
  189.         break;
  190.     case KEY_ESC:    /* Exit when the Escape key is pressed */
  191.         exit(0);
  192.     }
  193. }
  194.  
  195. GLvoid 
  196. specialkeys( GLint key, GLint x, GLint y )
  197. {
  198.     switch (key) {
  199.     case GLUT_KEY_F1:    /* Function key #1 */
  200.         /* print help information */
  201.         printHelp( progname );
  202.         break;
  203.     }
  204. }
  205.  
  206. GLvoid 
  207. mouse( GLint button, GLint state, GLint x, GLint y )
  208. {
  209.     if (state == GLUT_DOWN) {
  210.         switch (button) {
  211.         case GLUT_LEFT_BUTTON:
  212.             action = MOVE_EYE;
  213.             break;
  214.         case GLUT_MIDDLE_BUTTON:
  215.             action = TWIST_EYE;
  216.             break;
  217.         case GLUT_RIGHT_BUTTON:
  218.             action = ZOOM;
  219.             break;
  220.         }
  221.  
  222.         /* Update the saved mouse position */
  223.         xStart = x;
  224.         yStart = y;
  225.     } else {
  226.         action = MOVE_NONE;
  227.     }
  228.  
  229. }
  230.  
  231. GLvoid
  232. motion( GLint x, GLint y )
  233. {
  234.     switch (action) {
  235.     case MOVE_EYE:
  236.         /* Adjust the eye position based on the mouse position */
  237.         azimAngle += (GLdouble) (x - xStart);
  238.         incAngle -= (GLdouble) (y - yStart);
  239.         break;
  240.     case TWIST_EYE:
  241.         /* Adjust the eye twist based on the mouse position */
  242.         twistAngle = fmodf(twistAngle+(x - xStart), 360.0);
  243.         break;
  244.     case ZOOM:
  245.         /* Adjust the eye distance based on the mouse position */
  246.         distance -= (GLdouble) (y - yStart)/10.0;
  247.         break;
  248.     default:
  249.         printf("unknown action %d\n", action);
  250.     }
  251.     
  252.     /* Update the stored mouse position for later use */
  253.     xStart = x;
  254.     yStart = y;
  255.  
  256.     glutPostRedisplay();
  257. }
  258.  
  259. void
  260. resetView( GLvoid )
  261. {
  262.     distance = near + (far - near) / 2.0;
  263.     twistAngle = 0.0;    /* rotation of viewing volume (camera) */
  264.     incAngle = 0.0;
  265.     azimAngle = 0.0;
  266. }
  267.  
  268. void
  269. polarView( GLfloat distance, GLfloat azimuth, GLfloat incidence,
  270.             GLfloat twist)
  271. {
  272.     glTranslatef( 0.0, 0.0, -distance);
  273.     glRotatef( -twist, 0.0, 0.0, 1.0);
  274.     glRotatef( -incidence, 1.0, 0.0, 0.0);
  275.     glRotatef( -azimuth, 0.0, 0.0, 1.0);
  276. }
  277.  
  278. GLvoid
  279. drawScene( GLvoid )
  280. {
  281.     static GLfloat    year = 45.0, day = -90.0;
  282.  
  283.     static GLfloat  yellow[] = { 1.0, 1.0, 0.0, 1.0 };
  284.     static GLfloat  blue[] = { 0.0, 0.0, 1.0, 1.0 };
  285.     static GLfloat  gray[] = { 0.2, 0.2, 0.2, 1.0 };
  286.  
  287.     glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
  288.  
  289.     glPushMatrix();
  290.  
  291.         /* set up viewing transformation */
  292.         polarView( distance, azimAngle, incAngle, twistAngle );
  293.  
  294.         /* draw sun */
  295.  
  296.         glPushMatrix();
  297.  
  298.             /* Give the sun a yellow glow */
  299.             glColor4fv( yellow );
  300.           
  301.             /* rotate on our own axis */
  302.             glRotatef( 90.0, 1.0, 0.0, 0.0 );
  303.             if (filledFlag)
  304.                 glutSolidSphere( sunRadius, 15, 15 );
  305.             else
  306.                 glutWireSphere( sunRadius, 15, 15 );
  307.  
  308.         glPopMatrix();
  309.  
  310.         glPushMatrix();
  311.  
  312.             /* draw earth */
  313.  
  314.             /* rotate to the right time of year */
  315.             glRotatef( year, 0.0, 1.0, 0.0 );
  316.  
  317.             /* translate out to our orbit about the sun */
  318.             glTranslatef( earthOrbit, 0.0, 0.0 );
  319.             glPushMatrix();
  320.  
  321.                 /* set color for the earth */
  322.                 glColor4fv( blue );
  323.  
  324.                 /* rotate on our own axis */
  325.                 glRotatef( 90.0, 1.0, 0.0, 0.0 );
  326.                 if (filledFlag)
  327.                     glutSolidSphere( earthRadius, 15, 15 );
  328.                 else
  329.                     glutWireSphere( earthRadius, 15, 15 );
  330.                 
  331.             glPopMatrix();
  332.  
  333.             /* draw moon */
  334.             glPushMatrix();
  335.  
  336.                 /* set color for the moon */
  337.                 glColor4fv( gray );
  338.  
  339.                 /* rotate to the right time of day */
  340.                 glRotatef( day, 0.0, 1.0, 0.0 );
  341.  
  342.                 /* translate out to our orbit about the earth */
  343.                 glTranslatef( moonOrbit, 0.0, 0.0 );
  344.  
  345.                 /* rotate on our axis */
  346.                 glRotatef( 90.0, 1.0, 0.0, 0.0 );
  347.                 if (filledFlag)
  348.                     glutSolidSphere( moonRadius, 15, 15 );
  349.                 else
  350.                     glutWireSphere( moonRadius, 15, 15 );
  351.  
  352.             glPopMatrix();
  353.  
  354.         glPopMatrix();
  355.  
  356.     glPopMatrix();
  357.  
  358.     checkError( "drawScene" );
  359.  
  360.     glFlush();
  361. }
  362.